<template>
  <div class="basic-info">
    <Title :title="title">
      <slot />
    </Title>
    <div style="padding: 0 6px;">
      <el-row :gutter="gutter">
        <div v-if="!isAppendix">
          <el-empty v-if="list.length === 0" :description="description" />
          <el-col
            v-for="(item, index) in list"
            :key="index"
            :span="item.span || 12"
            style="margin-top: 22px;"
            :style="item.itemStyle"
          >
            <span>{{ item.label }}：</span>
            <span :style="item.style">{{ item.value }}</span>
          </el-col>
        </div>
        <div v-else>
          <el-empty v-if="list.length === 0" :description="description" />
          <div v-else>
            <el-col :span="24">
              <span style="padding: 10px 0">
                注：SF附件可点击预览后在预览页面进行下载；OMS附件中图片与PDF支持预览，其他不支持预览
              </span>
            </el-col>
            <el-col
              v-for="(item, index) in list"
              :key="index"
              :span="item.span || 12"
              style="margin-top: 16px;"
              :style="item.itemStyle"
            >
              <el-col :span="24">
                {{ isSfType(item.attachmentUrl)?`[SF] ${item.filename}`:`[OMS] ${item.filename}` }}
                <el-button
                  v-if="isSfType(item.attachmentUrl)"
                  type="text"
                  :style="{
                    marginLeft: '25px',
                    color: '#0072FF',
                    cursor: 'pointer',
                  }"
                  @click="previewHandle(item.attachmentUrl,item.filename)"
                >
                  预览
                </el-button>
                <el-button
                  v-if="!isSfType(item.attachmentUrl)&&isPreviewType(item.filename)"
                  type="text"
                  :style="{
                    marginLeft: '25px',
                    color: '#0072FF',
                    cursor: 'pointer',
                  }"
                  @click="previewHandle(item.attachmentUrl,item.filename)"
                >
                  预览
                </el-button>
                <a
                  v-if="!isSfType(item.attachmentUrl)"
                  :href="item.attachmentUrl"
                  :style="{
                    marginLeft: '10px',
                    color: '#0072FF',
                    cursor: 'pointer',
                  }"
                >
                  <el-button
                    type="text"
                  >
                    下载
                  </el-button>
                </a>

                <el-popconfirm
                  :style="{
                    margin: '0',
                    padding: '0',
                  }"
                  title="是否确认删除该附件？"
                  @confirm="removeFile(item.id)"
                >
                  <el-button
                    v-if="!isSfType(item.attachmentUrl)"
                    slot="reference"
                    type="text"
                    :style="{
                      margin: '0',
                      padding: '0',
                      marginLeft: '10px',
                    }"
                  >
                    <span
                      :style="{
                        color: '#F56C6C',
                      }"
                    >删除</span>
                  </el-button>
                </el-popconfirm>
              </el-col>
            </el-col>
          </div>
        </div>
      </el-row>
    </div>

    <BlockDialog v-if="curUrl" :url="curUrl" title="预览" :name="curName" @close="closeHandle" />
  </div>
</template>

<script>
import Title from '@/components/Title'
import { deleteAttachMent } from '@/service/ship'
import { isSfType, isPreviewType } from '@/utils/tools'
import BlockDialog from '@/components/BlockDialog'
export default {
  name: 'InfoCard',
  components: {
    Title,
    BlockDialog
  },
  props: {
    title: {
      type: String,
      required: true,
      default: () => ''
    },
    list: {
      type: Array,
      required: true,
      default: () => []
    },
    isAppendix: {
      type: Boolean,
      default() {
        return false
      }
    },
    description: {
      type: String,
      default() {
        return ''
      }
    }
  },
  data() {
    return {
      gutter: 12,
      curUrl: '',
      curName: '',
      isSfType,
      isPreviewType
    }
  },
  methods: {
    async removeFile(id) {
      await deleteAttachMent({ id })
      this.$message({
        type: 'success',
        message: '删除成功'
      })
      this.$emit('rePainDetail')
    },
    // 预览
    previewHandle(curUrl, curName) {
      // SF全部是新建窗口打开
      if (isSfType(curUrl)) {
        window.open(curUrl)
        return
      }
      // SF全部是新建窗口打开
      if (isPreviewType(curName)) {
        this.curUrl = curUrl
        this.curName = curName
      } else {
        window.location.href = curUrl
      }
    },
    // 清除url
    closeHandle() {
      this.curUrl = ''
      this.curName = ''
    }
  }
}
</script>

<style scoped lang="scss">
.basic-info {
  font-size: 14px;
  header {
    padding: 6px;
    font-weight: bold;
    background: #eee;
  }
}
</style>
